
clear
use "/home/projects/cedsei/data/rawdata/fek_1990"

foreach num of numlist 1991(1)2009 {
append using "/home/projects/cedsei/data/rawdata/fek_`num'"
}

*variable imputeradeforetag is str2 in master but double in using data
rename imputeradeforetag imputeradeforetag_string
foreach num of numlist 2010(1)2018 { 
append using "/home/projects/cedsei/data/rawdata/fek_`num'"
}
desc
tab year
{ // renaming variables
rename juridiskform      legal_form 
rename bransch_sni92   indcode_SNI92
rename nettoomsattning inc_NetTurnover
rename summapersonal~r inc_perscoststot
rename kostnaderforl~n inc_wages_other
rename socialakostna~l   inc_ss_other
rename summarorelsek~r inc_opexptot
rename summaavochned~r inc_deprec_amort_tot
rename rorelseresultat inc_opprofit
rename resultatfranf~i profitsfininv
rename resultatefter~r inc_profitafterfin
rename extraordinara~r inc_extraorditems
rename summaboksluts~r inc_appropriationstot
rename summaimmateri~l bs_intangibleassetstot
rename byggnaderochm~k bs_propertytot
rename maskinerochin~r bs_plantmachequip
rename summamateriel~a  bs_tangibleassetstot
rename andrafinansie~g bs_finassetsother
rename summafinansie~g bs_finassetstot
rename summaanlaggni~r bs_fixedassetstot
rename varulager       bs_inventories
rename kundfordringar  bs_moneytobereceived 
rename summako~dringar bs_moneytobereceived_short
rename summako~eringar bs_moneyplaced
rename kassaochbank   bs_cashandbank
rename summaomsattni~r bs_currentassetstot
rename summatillgangar bs_assetstot
rename aktiekapital    bs_sharecapital
rename ovrigtbundete~l bs_otherrestrcapital
rename aretsresultat   inc_netincome
rename ackumuleradeo~r bs_acceldepreciation
rename summaobeskatt~r bs_untaxedreserves
rename summalangfris~r bs_longtermliab
rename forskottfrank~r bs_advancepaymentfrcust
rename leverantorssk~r bs_inputliabilities
rename skatteskulder   bs_taxliab
rename summakortfri~er bs_currentliab
rename summaegetkapi~r bs_equityliabtot
rename periodisering~d bs_taxallocreserve
*is the next one really " inc_ChangeInventorAndCntractWork"?
rename forandringavl~t inc_chginventanwork
rename aktiverata~g inc_ActivatedOwnAccountWork
rename ovrigarorel~ter inc_otherrev
* not sure about next one
rename summakostnade~e inc_costsrawmatsandresale
rename ovrigaexterna~r inc_otherextcosts
rename ovrigapersona~r loc_otherpersonnelcost
rename ovrigarorel~der inc_otheropcosts
** not sure about the next one.. couldn't translate last part (KonK?) is this just dividends?s
rename utdelningpaak~c inc_dividendsonshrsandkoncern
rename ovrkortfristi~r bs_othershorttermreceivables
rename forutbetkostn~t bs_prepaidexpensesaccrinc
rename ovrigaobeskat~r bs_otheruntaxedreserves
rename avsattningarf~r bs_provisionspensioners
rename ovrigaavsattn~r bs_provisionsother
rename summaavsattni~r bs_provisionstot
rename upplupnakostn~a bs_accrexpensesprepaidinc
rename ovrigakortfri~r bs_othershortliab
rename bransch_sni2002 incode_sni2002
**
rename kostnaderforo~0 xx_costsforothersocialfee
rename skattekostnader xx_taxcosts
rename redovisatresultat xx_reportedresult
rename pensionskostn~r x_pensioncosts
rename resultatfrano~l x_incomefromotherfin
rename nedskrivninga~n inc_writeoffsoffinass
rename bransch_sni2007 incode_sni2007
}
gen inc_op_rev  = inc_NetTurnover + inc_otherrev + inc_chginvent
gen inc_op_cos  = inc_ActivatedOwnAccountWork + inc_perscosts + inc_costsrawmat + inc_deprec_am + inc_otherextc + inc_otheropco
gen inc_op_pr   = inc_op_rev + inc_op_cos
gen di = inc_op_pr - inc_opprofit
sum di, d 
drop di 

gen cf_op = inc_op_pr - inc_deprec_am - inc_chginvent

egen firm_start = min(year), by(lopnr_peorgnr ) 
// there are spikes in the nr of firms entering the data in 1997 and 2001. 
// We dont drop any firms at this time.
order lopnr_peorgnr year legal_form imputeradef* indco* incode* firm_start
qui foreach var of varlist inc_NetTurnover-cf_op {
	replace `var' = `var' /1000
}

gen value_added = inc_NetTurnover + inc_costsrawmatsandresale
gen profit_margin = inc_opprofit / value_added

xtset lopnr_peorgnr  year
save  "$datapath/balancesheet.dta", replace

qui {
keep if inlist(legal_form, "49")
// keep if agkat == 41 // DS What happens if a firm changes ownership category?
// DS this variable comes from rams. 
}


*Rename variables
rename lopnr_peorgnr firmid


save  "$datapath/balancesheet_restricted", replace
